var skdr = {
    setBtnStatus: function () {
        var row = $('#grid_skdr').datagrid('getSelected');
        if (row && row.id) {
            //未导入
            if (row.czzt == 'wdr') {
                $("#btn_skdr_import").show();//导入
                $("#btn_skdr_cx").hide();//撤销
                $("#btn_skdr_cz").hide();//重置
            }
            if (row.czzt == 'drcg') {
                $("#btn_skdr_import").hide();//导入
                $("#btn_skdr_cx").show();//撤销
                $("#btn_skdr_cz").hide();//重置
            }
            if (row.czzt == 'drsb') {
                $("#btn_skdr_import").hide();//导入
                $("#btn_skdr_cx").hide();//撤销
                $("#btn_skdr_cz").show();//重置
            }
            if (row.czzt == 'sccg') {
                $("#btn_skdr_import").show();//导入
                $("#btn_skdr_cx").hide();//撤销
                $("#btn_skdr_cz").hide();//重置
            }
            if (row.czzt == 'scsb') {
                $("#btn_skdr_import").hide();//导入
                $("#btn_skdr_cx").hide();//撤销
                $("#btn_skdr_cz").show();//重置
            }
        } else {
            $("#btn_skdr_import").hide();//导入
            $("#btn_skdr_cx").hide();//撤销
            $("#btn_skdr_cz").hide();//重置
        }
    }
}
var clickRowIndex = null;

$(function () {
    //加载企业机构
    $("#skdr_gldw").gsjgqczcombobox();
    //表格初始加载
    $('#grid_skdr').datagrid({
        url: $.webPath() + "/cwgl/skdr/skdrList",
        toolbar: "#toolbar_skdr",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'bm', title: '单据号', width: 25, align: 'center', formatter: skdrCell},
                {field: 'pjhm', title: '票据号码', width: 25, align: 'center'},
                {field: 'sfkrq', title: '收款日期', width: 25, align: 'center'},
                /*{field: 'sfklx', title: '收款类型', width: 15,
                 formatter: function (value) {
                 if (value == '3')
                 return '销售收款';
                 else
                 return '供应退款';
                 }, align: 'center'
                 },*/
                {field: 'gldw_qc', title: '关联企业', width: 40, align: 'center'},
                {field: 'sfje', title: '收款金额(元)', width: 25,
                    formatter: function (value) {
                        var sl = formatJe2(value, ",");
                        return sl;
                    },
                    styler: function () {
                        return 'color:green;font-size:14px;';
                    }, align: 'right'
                },
                /* {field: 'kxlx', title: '款项类型', width: 15,
                 formatter: function (value) {
                 if (value == '1')
                 return '预付款';
                 if (value == '2')
                 return '应付款';
                 if (value == '3')
                 return '预收款';
                 if (value == '4')
                 return '应收款';
                 }, align: 'center'
                 },*/
                {field: 'pjlx_z', title: '票据类型', width: 15, align: 'center'},
                {field: 'kxsy_z', title: '款项用途', width: 15, align: 'center'},
                {field: 'fkfs_z', title: '付款方式', width: 15, align: 'center'},
                {field: 'sbgs_jc', title: '申报公司', width: 30, align: 'center'},
                {field: 'sbr_xm', title: '申报人', width: 15, align: 'center'},
                /* {field: "bz", title: "备注", width: 20, align: 'center'},*/
                {field: "shrq", title: "审核日期", width: 20, align: 'center'} ,

                {field: "zt", title: "状态", width: 10,
                    formatter: function (value) {
                        if (value == 'ybc') {
                            return '已保存';
                        } else if (value == 'yrz') {
                            return '已入账';
                        } else if (value == 'ysh') {
                            return '已审核';
                        } else if (value == 'ytj') {
                            return '已提交';
                        } else if (value == 'ysd') {
                            return '已锁定';
                        } else if (value == 'yjs') {
                            return '已解锁';
                        } else if (value == 'yth') {
                            return '已退回';
                        } else if (value == 'ytz') {
                            return '已调整';
                        }
                    }, align: 'center'},
                /*    {field: 'sfhz', title: '是否划转', width: 15,
                 formatter: function (value) {
                 if (value == '0')
                 return '未划转';
                 if (value == '1')
                 return '已划转';

                 }, align: 'center'
                 },*/
                {field: 'insertFlag', title: '是否导入', width: 15,
                    formatter: function (value) {
                        if (value == 1)
                            return  '未导入';
                        else if (value == 2) {
                            return  '导入开始';
                        } else if (value == 3) {
                            return '导入成功';
                        }
                    }, align: 'center'
                }

            ]
        ],
        onLoadSuccess: function () {
            var rows = $(this).datagrid("getRows");
            if (rows.length > 0) {
                $(this).datagrid("selectRow", 0);
                clickRowIndex = 0;
            }
            skdr.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow: function (rowIndex, rowData) {
            skdr.setBtnStatus();
            if (clickRowIndex == rowIndex)
                return;
            clickRowIndex = rowIndex;
        },
        onSelect: function (rowIndex, rowData) {
            skdr.setBtnStatus();
            if (clickRowIndex == rowIndex)
                return;
            clickRowIndex = rowIndex;
            skdr.alwayShowDetail(rowData);
        }
    });
    //**********按钮触发事件**********

    //查询按钮
    $("#btn_skdr_search").click(function () {
        skdr_search();
    });

    //加载查询条件中的申报公司下拉框
    $("#skdr_sbgs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });

    // 财务收付导入按钮
    $("#btn_skdr_import").click(function () {
        skdr_insert();
    });
});

// 导入窗口
function skdr_insert() {
    var row = $('#grid_skdr').datagrid('getSelected');
    if (row) {
        var win = $.window({
            title: "收货款导入凭证",
            width: 1100,
            height: 500,
            cache: false,
            url: $.webPath() + "/cwgl/skdr/skdr_content?skdrId=" + row.id,
            modal: true,
            buttons: [
                {
                    text: '确认',
                    handler: function () {
                        $(win).mask();
                        $.ajax({
                            url: $.webPath() + "/cwgl/skdr/generate_voucher",
                            dataType: 'json',
                            type: 'POST',
                            data: {
                                "attachment": $("#skdr_attachment").val(),
                                "skdrId": $("#skdrId").val()},
                            beforeSend: function () {
                                if ($("#skdr_attachment").val() == "" || $("#skdr_attachment").val() == null) {
                                    $.messager.alert("提示", "请填写附单据数", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                            },
                            success: function (msg) {
                                $(win).mask('hide');
//                                $("#layout_bzjdr").mask("hide");
                                $(win).dialog('close');
                                if (msg.responseflag == 0) {
                                    $('#grid_bzjdr').datagrid('reload');
                                    $.messager.alert("提示", msg.responsemessage, "error");
                                } else {
                                    $('#grid_bzjdr').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: msg.responsemessage,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            }
                        });
                    }
                }
            ]
        });
    } else {
        $.messager.alert('提示', '请选择一行数据', 'error');
    }
}

//查询收款管理
function skdr_search() {
    var sbgs = $('#skdr_sbgs').combotree('getText') == null ? null : $('#skdr_sbgs').combotree('getText');
    var sbgsId = $('#skdr_sbgs').combotree('getValue') == null ? null : $('#skdr_sbgs').combotree('getValue');
    var skdrZt = $('#skdr_zt').combobox('getValue') == null ? null : $('#skdr_zt').combobox('getValue');
    var skshStart = $('#skdr_start').datebox('getValue');
    var skshEnd = $('#skdr_end').datebox('getValue');
    $('#grid_skdr').datagrid('load',
        {
            bm: $('#skdr_bm').val(),
            bzjdrpjlxMc: $("#skdr_pjlx_id").val() || '',
            sbgs: sbgs,
            sbgsId: sbgsId,
            gldwMc: $('#skdr_gldw').attr("_text") == null ? null : $('#skdr_gldw').attr("_text"),
            skdrZt: skdrZt,
            skr: $('#skdr_zdr').val(),
            skshStart: skshStart,
            skshEnd: skshEnd
        }
    );
}

